import store from "../../../store";
import {
	computed
} from "vue";
export default function useAvatar($http) {
	const userInfo = computed(() => store.state.userInfo)
	const _uploadFile = async (filePath, cloudPath) => {
		const {
			fileID
		} = await uniCloud.uploadFile({
			filePath,
			cloudPath
		});
		return fileID;
	}
	//更改用户的头像
	const _updataUserAvatar = async (filePath) => {
		const {
			msg
		} = await $http.updata_user_avatar({
			userId: userInfo.value._id,
			filePath
		})
		uni.showToast({
			title: msg,
			icon: 'none'
		});
		//修改本地数据
		store.commit('updateUserInfo', {
			...userInfo.value,
			avatar: filePath
		});
	};
	uni.chooseImage({
		count: 1,
		success: async res => {
			//上传图片
			const filePath = await _uploadFile(res.tempFilePaths[0], res.tempFiles[0].name)
			//更改用户的头像
			await _updataUserAvatar(filePath);
		}
	})
}